<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        /* 防止越界，遍历需要遍历到st.length - 1,
        剩下的在外部进行添加 */
        const st = 'assbbbbsdd'
        let i = 0, sum = 1;
        let result = ''
        for (i; i < st.length - 1; i++) {
            if (st[i] == st[i + 1]) {
                sum++;
            } else {
                result = result + st[i] + sum
                sum = 1
            }
        }
        result = result + st[i] + sum;
        if (result.length > st.length) {
            console.log(st);
        } else {
            console.log(result);
        }

    </script>
</body>

</html>